// pages/mine/index.js
import { getUserInfo } from '../../api/user.js'
import { getPhoneInfo } from '../../api/common.js'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showLoading: true, // 是否显示加载
    serviceList:[
      { img: '/static/img/common_icon/mine_order.png', name: '我的订单', title: '历史订单' },
      { img: '/static/img/common_icon/mine_information.png', name: '参赛信息', title: '参赛信息编辑' },
      { img: '/static/img/common_icon/mine_medal.png', name: '我的勋章', title: '千里之行的印记' },
      { img: '/static/img/common_icon/mine_integral.png', name: '我的积分', title: '签到领取积分' },
      { img: '/static/img/common_icon/mine_coupon.png', name: '领券中心', title: '足下优惠券' },
      // { img: '/static/img/common_icon/mine_qualifications.png', name: '我的资质', title: '活动赛事资质' },
      { img: '/static/img/common_icon/mine_support.png', name: '关于足下', title: '厉川技术支持' },
      { img: '/static/img/common_icon/mine_service_call.png', name: '联系客服', title: '客服在线解惑' },
    ],
    servicePhoneList:[],
    showServicePhone:false,
    isRequst:false,
    user:{},
    settingInfo:{}
  },
  onLoad: function (options) {
  },
  onShow: function () {
    this.feach()
  },
  feach(){
    this.setData({ isRequst: true })
    let { showLoading} = this.data
    getUserInfo(showLoading).then(res => {
      this.setData({...res.data, isRequst: false})

      app._getSettingInfo(data => {
        this.setData({ settingInfo: data })
      })
      if (showLoading){
        this.setData({ showLoading:false})
      }
    })
  },

  onImgOK(e) {
    this.setData({ sharePath: e.detail.path, visible: true })
    //通知外部绘制完成，重置isCanDraw为false
    this.triggerEvent('initData');
  },
  _getUserInfo(){
    // if (!wx.getStorageSync('userInfo')) {
      wx.navigateTo({
        url: '/pages/get_user_info/index',
      })
    // }
  },
  getService(e){
    if (!this.data.isRequst) {
      switch (Number(e.currentTarget.dataset.index)) {
        case 0:
            wx.navigateTo({ url: '/pages/order/index' })
          break;
        case 1:
          wx.navigateTo({ url: '/pages/participant_news/index' })
          break;
        case 2:
          wx.navigateTo({ url: '/pages/medal/index' })
          break;
        case 3:
          wx.navigateTo({ url: '/pages/integral_check/index?num=' + this.data.user.surplusIntegral })
          break;
        case 4:
          wx.navigateTo({ url: '/pages/get_ticket/index' })
          break;
        case 5:
          if (this.data.settingInfo) {
            app._getSettingInfo(data => {
              wx.navigateTo({ url: '/' + this.data.settingInfo.aboutZx })
            })
          } else {
            app._getSettingInfo(data => {
              wx.navigateTo({ url: '/' + data.aboutZx })
            })
          }
          break;
        case 6:
          this._getPhoneInfo()
          break;
        default:
          wx.navigateTo({ url: '/pages/live_center/index' })
          break;
      }
    }
  },
  _getPhoneInfo(){
    if (this.data.servicePhoneList.length > 0) {
      this.servicePhone(this.data.servicePhoneList)
    } else {
      getPhoneInfo().then(res => {
        let arr = [];
        if (res.data.length>0){
          res.data.forEach(e=>{
            arr.push(e.phone)
          })
        }
        this.setData({ servicePhoneList: arr})
        this.servicePhone(arr)
      })
    }
  },
  servicePhone(list){
    wx.showActionSheet({
      itemList: ['客服热线', ...this.data.servicePhoneList],
      success:res=>{
        if (res.tapIndex > 0) {
          wx.makePhoneCall({
            phoneNumber: this.data.servicePhoneList[res.tapIndex - 1]
          })
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.feach()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})